//
//  CMNetwork.m
//  DVActivity
//
//  Created by pan Shiyu on 13-5-6.
//  Copyright (c) 2013年 pan Shiyu. All rights reserved.
//

#import "CMNetwork.h"
#import "JSONKit.h"

@implementation CMNetwork

+ (void)getPath:(NSString *)path
     parameters:(NSDictionary *)parameters
        success:(void (^)(NSDictionary *result, NSData *responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
    
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    [httpClient getPath:nil
             parameters:parameters
                success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    NSDictionary *result = [(NSData *)responseObject objectFromJSONData];
                    success(result,responseObject);
                }
                failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    failure(operation,error);
                }];
}

+ (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(void (^)(NSDictionary *result))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
    
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:nil]];
    [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
    
    [httpClient postPath:path
              parameters:parameters
                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
                     NSDictionary *result = [(NSData *)responseObject objectFromJSONData];
                     success(result);
                 }
                 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                     failure(operation, error);
                 }];
}

@end
